import java.util.Arrays;
import java.util.PropertyResourceBundle;

public class MyArrayList implements IList{
    public int[] elam;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;

    public MyArrayList() {
        this.elam = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        boolean a = isFull();
        if(a == true){
            grow();
        }
        this.elam[usedSize] = data;
        usedSize++;

    }
    private void grow(){
        this.elam = Arrays.copyOf(this.elam,2*this.elam.length);
    }
    private boolean isFull(){
        return this.usedSize == this.elam.length;
    }

    @Override
    public void add(int pos, int data) {
        checkPos(pos,"pos方法的下标不合法");
        if(pos == usedSize){
            this.elam[pos] = data;
            usedSize++;
            return;
        }
        boolean a = isFull();
        if(a == true){
            grow();
        }
        for(int i = usedSize - 1; i >= pos; i--){

            this.elam[i+1] = this.elam[i];
        }
        this.elam[pos] = data;
        usedSize++;
    }
    private void checkPos(int pos,String messge){
        if(pos < 0 || pos > usedSize){
            throw new PosIllegality(messge);
        }
        return;
    }

    @Override
    public boolean contains(int toFind) {
        for(int i =0; i < usedSize; i++){
            if(this.elam[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i = 0; i < usedSize; i++){
            if(this.elam[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(isEmpty()){
            throw  new EmptyListException("数组为空");
        }
        check(pos,"pos方法的下标不合法");
        return this.elam[pos];
    }
    private void check(int pos,String mes){
        if(pos >= usedSize || pos < 0){
            throw new PosIllegality();
        }
    }
    private boolean isEmpty(){
       return usedSize == 0;
    }

    @Override
    public void set(int pos, int value) {
        if(isEmpty()){
            throw new EmptyListException("数组为空");
        }
        check(pos,"下标越界了");
        this.elam[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if(isEmpty()){
            throw new EmptyListException("数组为空");
        }
        int index = indexOf(toRemove);
        if(index == -1){
            return;
        }
        for (int i = index; i < usedSize-1; i++) {
            elam[i] = elam[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < usedSize; i++){
            elam[i] = 0;
        }
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elam[i] + " ");
        }
        System.out.println();
    }
}


